/*global location: false, YAHOO: false, jQuery: false, not_default_columns: false, window: false, Results: false, RECORDS_PER_PAGE: false, ResultsColumnDefs: false, emptyMessage: false, filter_options: true, ResultsDataTableDeferred: false */

/*members css, getColumnSet, get_Column, html, key, keys, label, length, 
    nc, new_columns, push, rebuild, table, val, value, with_table
*/

/* begin: SelectList object -- when user adds or removes columns from table */
var SelectList = {

    new_columns: function (new_cols) {
        if (!new_cols) {
            this.nc = [];
        }
        this.nc = new_cols;
        return this;
    },

    value: function () {
        var selected_key = "",
			rm_list_css = jQuery("select#id_row_to_rm").css("display");
        if (rm_list_css === 'none') {
            selected_key = jQuery("select#id_row_to_add").val();
        } else {
            selected_key = jQuery("select#id_row_to_rm").val();
        }
        return selected_key;
    },

    get_Column: function (key) {
        for (var i = 0; i < this.nc.length; i += 1) {
            if (this.nc[i].key === key) {
                return this.nc[i];
            }
        }
    },

    rebuild: function () {

        var columns_set = ResultsDataTableDeferred.getColumnSet().keys,
			columns_set_keys = [],
			n, i, b,
			select_list_for_deletion = "",
			select_list_for_add = "";
        for (i = 0; i < columns_set.length; i += 1) {
            select_list_for_deletion += "<option value=\"" + columns_set[i].key + "\">" + columns_set[i].label + "</option>";
            columns_set_keys.push(columns_set[i].key);
        }
        jQuery("select#id_row_to_rm").html(select_list_for_deletion);

        for (i = 0; i < this.nc.length; i += 1) {
            b = false;
            for (n = 0; n < columns_set_keys.length; n += 1) {
                if (this.nc[i].key === columns_set_keys[n]) {
                    b = true;
                }
            }
            if (b) {
                continue;
            } else {
                select_list_for_add += "<option value=\"" + this.nc[i].key + "\">" + this.nc[i].label + "</option>";
            }
        }
        if (select_list_for_add === "") {
            select_list_for_add = "<option value=\"false\">No columns available</option>";
        }
        jQuery("select#id_row_to_add").html(select_list_for_add);
    }
};
/* end: SelectList */
